package tn.orange.canreminder.adapter;

import java.util.List;

import tn.orange.canreminder.R;
import tn.orange.canreminder.helper.CalendrierHelper;
import tn.orange.canreminder.model.Country;
import tn.orange.canreminder.sqlite.Equipe;
import tn.orange.canreminder.sqlite.EquipeDao;
import tn.orange.canreminder.sqlite.EquipeDaoImp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class EquipeAdapter extends BaseAdapter {

	private Context context;
	private List<Country> countries;
	private LayoutInflater mInflater;

	public EquipeAdapter(Context context, List<Country> countries) {
		super();
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.countries = countries;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return countries.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return countries.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.equipe_item, null);
			holder = new ViewHolder();
			holder.nom = (TextView) convertView
					.findViewById(R.id.equipe_item_nom);
			holder.img = (ImageView) convertView
					.findViewById(R.id.equipe_item_img);

			holder.fb = (ImageView) convertView.findViewById(R.id.login);

			holder.alarm = (TextView) convertView
					.findViewById(R.id.equipe_item_alarm);
			holder.btn = (ImageButton) convertView
					.findViewById(R.id.equipe_item_btn_alarm);

		} else
			holder = (ViewHolder) convertView.getTag();

		final Country country = countries.get(position);
		holder.fb.setTag(country);
		convertView.setTag(holder);
		String slug1 = country.getNameslug();
		holder.alarm.setTag(position);

		holder.img.setImageResource(getIdFromSlug(slug1));
		holder.nom.setText(country.getName());
		holder.btn.setTag(position);
		EquipeDao equipeDaoImp = new EquipeDaoImp(context);
		Equipe equipe = equipeDaoImp.getEquipebyName(country.getName());

		final String alarm = equipe.getType();
		if (alarm == null)
			holder.alarm.setText("alarme  désactivé");
		else {
			holder.alarm.setText("alarme "
					+ CalendrierHelper.getSelectedTime(alarm));
			convertView.findViewById(R.id.alarme).setVisibility(ImageView.GONE);
		}

		return convertView;
	}

	static class ViewHolder {
		TextView nom;
		ImageView img;
		ImageView box;
		ImageView fb;
		TextView alarm;
		ImageButton btn;

	}

	public static int getIdFromSlug(String slug) {
		slug = slug.replace("-", "");
		try {
			return R.drawable.class.getField(slug).getInt(null);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
}
